<template>
  <div class="scroll-container" ref="scrollContainer" @wheel.prevent="handleScroll">
    <div class="scroll-wrapper" ref="scrollWrapper" :style="{top: top + 'px'}">
      <slot></slot>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
const DELTA = 15

export default {
  data() {
    return {
      top: 0
    }
  },
  methods: {
    handleScroll(e) {
      const eventDelta = e.wheelDelta || -e.deltaY * 3
      const $container = this.$refs.scrollContainer
      const $containerHeight = $container.offsetHeight
      const $wrapper = this.$refs.scrollWrapper
      const $wrapperHeight = $wrapper.offsetHeight
      if (eventDelta > 0) {
        this.top = Math.min(0, this.top + eventDelta)
      } else {
        if ($containerHeight - DELTA < $wrapperHeight) {
          if (this.top >= -($wrapperHeight - $containerHeight + DELTA)) {
            this.top = Math.max(
              this.top + eventDelta,
              $containerHeight - $wrapperHeight - DELTA
            )
          }
        } else {
          this.top = 0
        }
      }
    }
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
@import '../../styles/variables.styl'
.scroll-container
  position relative
  width 100%
  height 100%
  background-color $MenuBackground
  .scroll-wrapper
    position absolute
    width 100% !important
</style>
